﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vds2465Telegrams;

namespace Vds2465Parser
{
    internal static class GenParse_ReportType_State
    {
        #region private constants

        internal const byte MsgBlockOnlyValue = 0x60;

        private const byte GeneralByte = 0x00;
        private const byte SecurityAreaByte = 0x01;
        private const byte InternalAreaByte = 0x02;
        private const byte RevisionStateByte = 0x03;
        private const byte DayModeByte = 0x04;

        #endregion private constants

        #region internal methods

        internal static byte GenerateByte(ReportType_State reportTypeState)
        {
            byte resultByte = 0x0;

            // State has 0x1 in Bits 6,5,4
            resultByte += MsgBlockOnlyValue;

            // add the report type sub type
            if (reportTypeState.ReportSubType == eReportSubType_State.General)
            {
                resultByte |= GeneralByte;
                return resultByte;
            }

            if (reportTypeState.ReportSubType == eReportSubType_State.SecurityArea)
            {
                resultByte |= SecurityAreaByte;
                return resultByte;
            }

            if (reportTypeState.ReportSubType == eReportSubType_State.InternalArea)
            {
                resultByte |= InternalAreaByte;
                return resultByte;
            }

            if (reportTypeState.ReportSubType == eReportSubType_State.RevisionState)
            {
                resultByte |= RevisionStateByte;
                return resultByte;
            }

            if (reportTypeState.ReportSubType == eReportSubType_State.DayMode)
            {
                resultByte |= DayModeByte;
                return resultByte;
            }

            // TODO handle error (unknown eReportSubType_State)
            return resultByte;
        }

        internal static bool ParseByte(byte byteValue, out eReportSubType_State reportSubTypeState)
        {
            // set arbitary default value
            reportSubTypeState = eReportSubType_State.General;

            // get only bits 6,5,4
            byte messageBlock = (byte)(byteValue & (byte)0x70);
            if (messageBlock == MsgBlockOnlyValue)
            {
                // get only bits 3,2,1,0
                byte subType = (byte)(byteValue & (byte)0x0f);
                if (subType == GeneralByte)
                {
                    reportSubTypeState = eReportSubType_State.General;
                    return true;
                }

                if (subType == SecurityAreaByte)
                {
                    reportSubTypeState = eReportSubType_State.SecurityArea;
                    return true;
                }

                if (subType == InternalAreaByte)
                {
                    reportSubTypeState = eReportSubType_State.InternalArea;
                    return true;
                }

                if (subType == RevisionStateByte)
                {
                    reportSubTypeState = eReportSubType_State.RevisionState;
                    return true;
                }

                if (subType == DayModeByte)
                {
                    reportSubTypeState = eReportSubType_State.DayMode;
                    return true;
                }

                // bits 3,2,1,0 are wrong
                return false;
            }

            // bits 6,5,4 are wrong
            return false;
        }

        #endregion internal methods
    }
}
